package com.fare.taluo.fetl.domain.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.fetl.domain.entity.AirChinaInternationalEntity;
import com.fare.taluo.fetl.domain.repository.IAirChinaInternationalRepository;

@Repository("airChinaInternationalRepository")
public class AirChinaInternationalRepositoryImpl extends
		EntityFetlHibernateRepository implements
		IAirChinaInternationalRepository {

	@Override
	public List<AirChinaInternationalEntity> findByCondition(Date reportDate,
			Date flightDate, String fromCityCode, String toCityCode,
			String flightNo, String carrierKey, String hbaseKey) {

		StringBuffer jpql =new StringBuffer("select airChina from AirChinaInternationalEntity airChina where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(reportDate!=null){
			jpql.append(" and airChina.reportDate =? ");
			conditionVals.add(reportDate);
		}
		if(flightDate !=null){
			jpql.append(" and airChina.flightDate =? ");
			conditionVals.add(flightDate);
		}
		if(null!=fromCityCode && !"".equals(fromCityCode)){
			jpql.append(" and airChina.fromCityCode =? ");
			conditionVals.add(fromCityCode);
		}
		if(null!=toCityCode && !"".equals(toCityCode)){
			jpql.append(" and airChina.toCityCode =? ");
			conditionVals.add(toCityCode);
		}
		if(null!=flightNo && !"".equals(flightNo)){
			jpql.append(" and airChina.flightNo =? ");
			conditionVals.add(flightNo);
		}
		if(null!=hbaseKey && !"".equals(hbaseKey)){
			jpql.append(" and airChina.hbaseKey =? ");
			conditionVals.add(hbaseKey);
		}
		if(null!=carrierKey && !"".equals(carrierKey)){
			jpql.append(" and airChina.carrierKey =? ");
			conditionVals.add(carrierKey);
		}
	
		jpql.append(" order by airChina.createTime desc ");
		Session session =getSessionFactory1().openSession();
		
		List<AirChinaInternationalEntity> list =null;
		try {
			Query query = session.createQuery(jpql.toString());
			for (int i = 0; i < conditionVals.size(); i++) {
				query = query.setParameter(i, conditionVals.get(i));
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		return list;
	}

}
